import { Body, Controller, Post, Get, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody, ApiQuery } from '@nestjs/swagger';
import { BaseService } from './base.service';

@ApiTags('微信授权')
@Controller('api/weixin/base')
export class BaseController {
  constructor(private readonly baseService: BaseService) {}

  @Post('/login')
  @ApiOperation({ summary: '小程序登录' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        code: { type: 'string', description: '小程序登录凭证' },
      },
    },
  })
  async wxLogin(@Body() body: { code: string }) {
    const { code } = body;
    return await this.baseService.wxLogin(code);
  }

  @Post('/accessToken')
  @ApiOperation({ summary: '获取网页授权access_token' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        code: { type: 'string', description: '授权码' },
      },
    },
  })
  async getAccessToken(@Body() body: { code: string }) {
    const { code } = body;
    return await this.baseService.getAccessToken(code);
  }

  @Post('/refreshToken')
  @ApiOperation({ summary: '刷新access_token' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        refresh_token: { type: 'string', description: '刷新令牌' },
      },
    },
  })
  async refreshAccessToken(@Body() body: { refresh_token: string }) {
    const { refresh_token } = body;
    return await this.baseService.refreshAccessToken(refresh_token);
  }

  @Post('/userInfo')
  @ApiOperation({ summary: '获取用户信息' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        access_token: { type: 'string', description: '访问令牌' },
        openid: { type: 'string', description: '用户openid' },
      },
    },
  })
  async getUserInfo(@Body() body: { access_token: string; openid: string }) {
    const { access_token, openid } = body;
    return await this.baseService.getUserInfo(access_token, openid);
  }

  @Post('/validateToken')
  @ApiOperation({ summary: '验证access_token有效性' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        access_token: { type: 'string', description: '访问令牌' },
        openid: { type: 'string', description: '用户openid' },
      },
    },
  })
  async validateAccessToken(
    @Body() body: { access_token: string; openid: string },
  ) {
    const { access_token, openid } = body;
    const isValid = await this.baseService.validateAccessToken(
      access_token,
      openid,
    );
    return { valid: isValid };
  }

  @Post('/decrypt')
  @ApiOperation({ summary: '解密小程序数据' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        encryptedData: { type: 'string', description: '加密数据' },
        sessionKey: { type: 'string', description: '会话密钥' },
        iv: { type: 'string', description: '初始向量' },
      },
    },
  })
  async decryptWxData(
    @Body() body: { encryptedData: string; sessionKey: string; iv: string },
  ) {
    const { encryptedData, sessionKey, iv } = body;
    return this.baseService.decryptWxData(encryptedData, sessionKey, iv);
  }

  @Get('/qrcode')
  @ApiOperation({ summary: '生成微信登录二维码链接' })
  @ApiQuery({ name: 'redirect_uri', description: '回调地址' })
  @ApiQuery({ name: 'state', description: '状态参数', required: false })
  async generateQRCodeUrl(
    @Query('redirect_uri') redirectUri: string,
    @Query('state') state?: string,
  ) {
    const qrCodeUrl = this.baseService.generateQRCodeUrl(redirectUri, state);
    return { qrCodeUrl };
  }
}
